<template>
  <div class="article">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>

    <!-- 文章详情 -->
    <div class="loading" v-if="loading">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          :src="article.aut_photo"
          round
          width="1rem"
          height="1rem"
          fit="cover"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | fromNow }}</p>
        </div>
        <van-button round size="small" type="info" @click="follow">
          {{ article.is_followed ? '取关' : '+ 关注' }}
        </van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
        <!-- <div>正文</div> -->
      </div>

      <!--分割线-->
      <van-divider>END</van-divider>

      <!--按钮-->
      <div class="btns">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          @click="likeGoods"
        >
          {{ article.attitude === 1 ? '取消' : '点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          :icon="article.attitude === 0 ? 'delete' : 'delete-o'"
          @click="dislikes"
        >
          {{ article.attitude === 0 ? '取消' : '不喜欢' }}
        </van-button>
      </div>
    </div>
    <!-- /文章详情 -->
    <Comment></Comment>
  </div>
</template>
<script>
import {
  getArtilceDetail,
  unFollowUser,
  followUser,
  deleteLike,
  addLike,
  deleteDisLike,
  addDisLike
} from '@/api/article'
import { mapState } from 'vuex'
import Comment from './comment'
export default {
  data() {
    return {
      article: {},
      loading: true
    }
  },

  async created() {
    const res = await getArtilceDetail(this.artId)
    console.log(res)
    this.article = res.data
    this.loading = false
  },
  components: {
    Comment
  },
  computed: {
    ...mapState('user', ['token']),
    // 获取文章id，在当前路由前就有该信息
    artId() {
      // .id 是当初在定路由的时候，起的名字
      return this.$route.params.id
    }
  },
  methods: {
    // 1. 判断用户是否登录
    // 2. 如果用户没有登录，跳转到登录
    // 3. 如果用户登录，判断用户是否关注
    // 4. 如果用户关注了，发请求取关
    // 5. 如果用户没关注，发请求关注
    async follow() {
      if (!this.token.token) {
        // 没有登录
        // this.$router.push('/login')
        // this.$toast('请先登录')

        // 带上回来的地址
        this.$router.push({
          path: '/login',
          query: {
            flag: true, // 有flag，就是登录成功的时候，使用back方式回来，不要使用push回来
            back: this.$route.fullPath // 回来的地址是啥 带过去
          }
        })
        return this.$toast('请先登录')
      }
      // 执行到这 ==> 已经登录过 ==> 发送请求实现关注还是取消关注操作
      // 具体调用哪个接口，通过 article.is_followed 数据来区分

      if (this.article.is_followed) {
        // 取消关注
        await unFollowUser(this.article.aut_id)
      } else {
        // 关注
        await followUser(this.article.aut_id)
      }

      // 提示
      this.$toast('操作成功')
      // 对 is_followed 取反
      this.article.is_followed = !this.article.is_followed
    },
    // 点赞
    async likeGoods() {
      if (!this.token.token) {
        // 没有登录
        // this.$router.push('/login')
        // this.$toast('请先登录')

        // 带上回来的地址
        this.$router.push({
          path: '/login',
          query: {
            flag: true, // 有flag，就是登录成功的时候，使用back方式回来，不要使用push回来
            back: this.$route.fullPath // 回来的地址是啥 带过去
          }
        })
        return this.$toast('请先登录')
      }

      if (this.article.attitude === 1) {
        await deleteLike(this.artId)
        this.article.attitude = -1
        this.$toast('取消点赞成功')
      } else {
        await addLike(this.artId)
        this.article.attitude = 1
        this.$toast('点赞成功')
      }
      // this.$toast.success('操作成功')
      // this.article.attitude = this.article.attitude === 1 ? -1 : 1
    },
    // 取消点赞
    async dislikes() {
      if (!this.token.token) {
        // 没有登录
        // this.$router.push('/login')
        // this.$toast('请先登录')

        // 带上回来的地址
        this.$router.push({
          path: '/login',
          query: {
            flag: true, // 有flag，就是登录成功的时候，使用back方式回来，不要使用push回来
            back: this.$route.fullPath // 回来的地址是啥 带过去
          }
        })
        return this.$toast('请先登录')
      }
      if (this.article.attitude === 0) {
        await deleteDisLike(this.artId)
      } else {
        await addDisLike(this.artId)
      }
      this.$toast('操作成功')
      this.article.attitude = this.article.attitude === 0 ? -1 : 0
    }
  }
}
</script>

<style lang="less" scoped>
.loading {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
  .btns {
    width: 50%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-content: center;
  }
}
</style>
